package com.ruoyi.common.file.util;

import com.obs.services.ObsClient;
import com.obs.services.model.ObjectListing;

import java.io.IOException;

/**
 * ClassName：TestMain
 * Package：com.ruoyi.common.oss.util
 * Description：
 *
 * @Author：fpy
 * @Create：2025/5/6 - 16:15
 * @Version：v1.0
 */
public class TestMain {
    /* 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。*/
    public static String accessKey = "HPUATMJLWSZGOSMBPNK4";
    public static String securityKey = "3TIGZVtcn9J4076uomTDqZYOX3ljcrV3VqSjqabR";

    public static void main(String[] args) throws IOException {


        // Endpoint以北京四为例，其他地区请按实际情况填写。
        // http://jiuren-ai-study.obs.cn-north-4.myhuaweicloud.com
        String endPoint = "https://obs.cn-north-4.myhuaweicloud.com";
        String bucketName = "jiuren-ai-study";
        // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY_ID。
        // 您可以登录访问管理控制台获取访问密钥AK/SK，获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(accessKey, securityKey, endPoint);

        // 待上传的本地文件路径，需要指定到具体的文件名
//        FileInputStream fis = new FileInputStream(new File("F:\\67b866637af859afe9689da2bf95bd8d.jpg"));
//        PutObjectResult putObjectResult = obsClient.putObject(bucketName, "67b866637af859afe9689da2bf95bd8d", fis);

//        String policyJson = "{\"Statement\":[{"
//                + "\"Principal\":\"*\","
//                + "\"Effect\":\"Allow\","
//                + "\"Action\":[\"GetObject\", \"DeleteObject\", \"PutObject\"],"
//                + "\"Resource\":\"" + bucketName + "/*"
//                + "\"}]}";

//        String policyJson = "{\"Statement\":[{\"Principal\":\"*\",\"Effect\":\"Allow\",\"Action\":\"ListBucket\",\"Resource\":\""
//                + bucketName
//                + "\"}]}";

//        String policyJson = "{ \n" +
//                "    \"Statement\":[ \n" +
//                "    { \n" +
//                "      \"Sid\":\"test\", \n" +
//                "      \"Effect\":\"Allow\", \n" +
//                "      \"Principal\": {\"ID\": [\"domain/b4bf1b36d9ca43d984fbcb9491b6fce9:user/71f3901173514e6988115ea2c26d1999\"]}, \n" +
//                "      \"Action\":[\"*\"], \n" +
//                "      \"Resource\":[\"examplebucket/*\",\"examplebucket\"]\n" +
//                "    } \n" +
//                "  ] \n" +
//                "}";

//        obsClient.setBucketPolicy(bucketName, policyJson);

        // 列举桶
//        List<ObsBucket> obsBuckets = obsClient.listBuckets(new ListBucketsRequest());
//        obsBuckets.forEach(obsBucket -> System.out.println(obsBucket.getBucketName()));

        // 列举对象
        ObjectListing objectListing = obsClient.listObjects(bucketName);
        objectListing.getObjects().forEach(obsObject -> System.out.println(obsObject.getObjectKey()));


//        String bucketPolicy = obsClient.getBucketPolicy("jiuren-ai-study");
//        System.out.println(bucketPolicy);

        // TODO 设置桶策略

        // 关闭obsClient
        obsClient.close();

    }


}
